﻿using AutoMapper;
using JuCheap.Core.Infrastructure;
using JuCheap.Core.Infrastructure.Exceptions;
using JuCheap.Core.IService;
using JuCheap.Core.IService.AppService;
using JuCheap.Core.Model;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace JuCheap.Core.WebApi.Controllers
{
    /// <summary>
    /// 公司服务api
    /// </summary>
    [Route("api/[controller]")]
    public class CompanyController : BaseApiController
    {
        private readonly ICompanyService _companyService;
        private readonly ISysCompanyService _sysCompanyService;
        private readonly IMapper _mapper;
        private readonly ILogger<CompanyController> _logger;

        /// <summary>
        /// ctor
        /// </summary>
        public CompanyController(ICompanyService companyService, ISysCompanyService sysCompanyService, IMapper mapper, ILogger<CompanyController> logger)
        {
            _companyService = companyService;
            _sysCompanyService = sysCompanyService;
            _mapper = mapper;
            _logger = logger;
        }

        /// <summary>
        /// 创建公司信息
        /// </summary>
        /// <param name="companyCreateReq"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("create")]
        public async Task<ActionResult<string>> Create([FromBody]CompanyCreateRequestDto companyCreateReq)
        {
            var companyId = await _companyService.CreateAsync(companyCreateReq);
            return Ok(companyId);
        }

        /// <summary>
        /// 分页获取公司信息
        /// </summary>
        [HttpPost]
        [Route("search")]
        public async Task<ActionResult<PagedResult<SysCompanyDto>>> Search([FromBody]SysCompanyFilter filter)
        {
            var records = await _companyService.QueryPagesAsync(filter);
            return Ok(records);
        }

        /// <summary>
        /// 获取公司信息
        /// </summary>
        [HttpGet]
        [Route("{id}")]
        public async Task<ActionResult<SysCompanyDto>> Get([FromRoute]string id)
        {
            var companyDto = await _sysCompanyService.FindAsync(id);
            return Ok(companyDto);
        }

        /// <summary>
        /// 修改公司信息
        /// </summary>
        /// <param name="companyEditRequest"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("edit")]
        public async Task<ActionResult<bool>> Edit([FromBody]CompanyEditRequestDto companyEditRequest)
        {
            await _companyService.UpdateAsync(companyEditRequest);
            return Ok(true);
        }

        /// <summary>
        /// 删除公司信息
        /// </summary>
        /// <param name="ids">id集合</param>
        /// <returns></returns>
        [HttpPut]
        [Route("delete")]
        public async Task<ActionResult<bool>> Delete([FromBody]IList<string> ids)
        {
            if (await _sysCompanyService.ExistsAsync(x => ids.Contains(x.Id) && x.IsSystem))
            {
                throw new BusinessException("系统运营公司不允许删除");
            }
            await _sysCompanyService.DeleteAsync(ids);
            return Ok(true);
        }
    }
}
